﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Abreu.Profile.ObjectClasses;
using System.Collections.ObjectModel;
using Abreu.Profile.Database;
using System.Data.Linq;

namespace Abreu.Profile.Classes
{
    public class Section : IDisposable 
    {
        #region Class Members
        CorpAbreuDataContext _corpAbreuDC = new CorpAbreuDataContext();
        #endregion

        #region Public Methods
        /// <summary>
        /// Gets the section profile info using all the items in the T_EmployeeUserSectionCategory of the user
        /// </summary>
        /// <param name="listProfileInfo"></param>
        /// <returns>Collection of SectionProfile</returns>
        public Collection<SectionProfile> GetSectionProfileByCollection(Collection<ProfileInfo> listProfileInfo)
        {
            Collection<SectionProfile> listSectionProfile = new Collection<SectionProfile>();
            Collection<SectionPropertiesProfile> listSectionProperties = new Collection<SectionPropertiesProfile>();

            foreach (ProfileInfo profileInfo in listProfileInfo)
            {
                //Get section Info with P_SectionInfo_Sel
                var sectionProfileInfo = _corpAbreuDC.P_GetSectionInfo(profileInfo.SectionId);
                //P_SectionInfo_SelResult resSectionInfo = (P_SectionInfo_SelResult)sectionProfileInfo;

                foreach (P_GetSectionInfoResult resSectionInfo in sectionProfileInfo)
                {
                    //Get business Info with P_BusinessInfo_Sel
                    var businessUnitInfo = _corpAbreuDC.P_GetBusinessUnitInfo(resSectionInfo.BusinessUnitId);
                    //P_BusinessUnitInfo_SelResult resBusinessUnitInfo = (P_BusinessUnitInfo_SelResult)businessUnitInfo;

                    foreach (P_GetBusinessUnitInfoResult resBusinessUnitInfo in businessUnitInfo)
                    {
                        //Get section properties info
                        listSectionProperties = GetSectionProperties(profileInfo.SectionId);

                        //Set Section Info
                        listSectionProfile.Add(new SectionProfile(resSectionInfo.DepartmentId, resSectionInfo.BusinessUnitId, resSectionInfo.NameSection,
                                                resSectionInfo.DocDefaultLanguageId, resSectionInfo.StatusSection,
                                                new BusinessUnitProfile(resBusinessUnitInfo.PrecedentBusinessUnitId, resBusinessUnitInfo.Code, resBusinessUnitInfo.NameBusinessUnit,
                                                                        resBusinessUnitInfo.BusinessUnitDescription, resBusinessUnitInfo.Level, resBusinessUnitInfo.StatusBusinessUnit)
                                                , listSectionProperties));
                    }
                }
            }

            //Returns collection
            return listSectionProfile;
        }

        /// <summary>
        /// Gets the section profile info using the isMaster item in the T_EmployeeUserSectionCategory of the user
        /// </summary>
        /// <param name="profileInfo"></param>
        /// <returns>SectionProfile struct</returns>
        public SectionProfile GetSectionProfile(ProfileInfo profileInfo)
        {
            SectionProfile sectionProfile = null;
            Collection<SectionPropertiesProfile> listSectionProperties = new Collection<SectionPropertiesProfile>();

            //Get section Info with P_SectionInfo_Sel
            var sectionProfileInfo = _corpAbreuDC.P_GetSectionInfo(profileInfo.SectionId);
            //P_SectionInfo_SelResult resSectionInfo = (P_SectionInfo_SelResult)sectionProfileInfo;

            foreach (P_GetSectionInfoResult resSectionInfo in sectionProfileInfo)
            {
                //Get business Info with P_BusinessInfo_Sel
                var businessUnitInfo = _corpAbreuDC.P_GetBusinessUnitInfo(resSectionInfo.BusinessUnitId);
                //P_BusinessUnitInfo_SelResult resBusinessUnitInfo = (P_BusinessUnitInfo_SelResult)businessUnitInfo;

                foreach (P_GetBusinessUnitInfoResult resBusinessUnitInfo in businessUnitInfo)
                {
                    //Get section properties info
                    listSectionProperties = GetSectionProperties(profileInfo.SectionId);

                    //Set Section Info
                    sectionProfile = new SectionProfile(resSectionInfo.DepartmentId, resSectionInfo.BusinessUnitId, resSectionInfo.NameSection,
                                             resSectionInfo.DocDefaultLanguageId, resSectionInfo.StatusSection,
                                             new BusinessUnitProfile(resBusinessUnitInfo.PrecedentBusinessUnitId, resBusinessUnitInfo.Code, resBusinessUnitInfo.NameBusinessUnit,
                                                       resBusinessUnitInfo.BusinessUnitDescription, resBusinessUnitInfo.Level, resBusinessUnitInfo.StatusBusinessUnit)
                                             , listSectionProperties);
                }
            }
            //Returns collection
            return sectionProfile;
        }

        /// <summary>
        /// Dispose members of class
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);

        }
        #endregion

        #region Private Methods
        private Collection<SectionPropertiesProfile> GetSectionProperties(int? sectionId)
        {
            Collection<SectionPropertiesProfile> listSectionProperties = new Collection<SectionPropertiesProfile>();

            //Get section properties info with P_SectionProperty_Sel
            var sectionPropertyInfo = _corpAbreuDC.P_GetSectionPropertyInfo(sectionId);

            foreach (P_GetSectionPropertyInfoResult resSectionPropertyInfo in sectionPropertyInfo)
            {
                //Get property section with P_PropertySection_Sel
                var propertyInfo = _corpAbreuDC.P_GetPropertySection(resSectionPropertyInfo.PropertyId);
                //P_PropertySection_SelResult resPropertyInfo = (P_PropertySection_SelResult)PropertyInfo;

                foreach (P_GetPropertySectionResult resPropertyInfo in propertyInfo)
                {
                    listSectionProperties.Add(new SectionPropertiesProfile(resSectionPropertyInfo.SectionId, resSectionPropertyInfo.PropertyId,
                                              resSectionPropertyInfo.Value, resSectionPropertyInfo.Notes,
                                              resSectionPropertyInfo.BeginDate, resSectionPropertyInfo.EndDate,
                                              new PropertySection(resPropertyInfo.PropertyCategoryId, resPropertyInfo.DataTypeId, resPropertyInfo.LanguageId,
                                                                  resPropertyInfo.Name, resPropertyInfo.Code, resPropertyInfo.DefaultValue, resPropertyInfo.IsValueRequired,
                                                                  resPropertyInfo.IsTranslationRequired, resPropertyInfo.StatusProperty)));
                }
            }

            //Return Collection
            return listSectionProperties;
        }

        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_corpAbreuDC != null)
                {
                    _corpAbreuDC.Dispose();
                    _corpAbreuDC = null;
                }
            }
        }
        #endregion
    }
}
